package com.aaa.qy127.dao.impl;

import com.aaa.qy127.dao.RukuCheckDao;
import com.aaa.qy127.entity.RukuCheck;
import com.aaa.qy127.util.JDBCUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class RukuCheckDaoImpl implements RukuCheckDao {
    /**
     * 查询等待审核的入库单
     * @param rukuCheck
     * @return
     */
    @Override
    public List<Map<String, Object>> queryAllruku(RukuCheck rukuCheck) {
        StringBuilder sql = new StringBuilder("select rukuid,rdate,staffName,totalprice,totalcount,jsr\n" +
                "from t_ruku r \n" +
                "join t_staff s\n" +
                "on s.id = r.uid where 1=1 and r.status = 0");
        if (rukuCheck.getStaffName()!=null && !"".equals(rukuCheck.getStaffName())){
            sql.append(" and staffName like '%").append(rukuCheck.getStaffName()).append("%'");
        }
        if (rukuCheck.getJsr()!=null && !"".equals(rukuCheck.getJsr())){
            sql.append(" and jsr like '%").append(rukuCheck.getJsr()).append("%'");
        }
        if (rukuCheck.getOnTime()!=null && !"".equals(rukuCheck.getOnTime())){
            sql.append(" and r.rdate >= '").append(rukuCheck.getOnTime()).append("'");
        }
        if (rukuCheck.getOffTime()!=null && !"".equals(rukuCheck.getOffTime())){
            sql.append(" and r.rdate <= '").append(rukuCheck.getOffTime()).append("'");
        }
        sql.append(" limit ?,?");
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql.toString(),(rukuCheck.getPage()-1)*rukuCheck.getLimit(),rukuCheck.getLimit());
        if(maps.size()>0){
            return maps;
        }
        return null;
    }
    /**
     * 查询订单审核入库单的数量
     * @param rukuCheck
     * @return
     */
    @Override
    public int getAllCount(RukuCheck rukuCheck) {
        StringBuilder sql = new StringBuilder("select count(rukuid)\n" +
                "from t_ruku r \n" +
                "join t_staff s\n" +
                "on s.id = r.uid where 1=1 and r.status = 0");
        if (rukuCheck.getStaffName()!=null && !"".equals(rukuCheck.getStaffName())){
            sql.append(" and staffName like '%").append(rukuCheck.getStaffName()).append("%'");
        }
        if (rukuCheck.getJsr()!=null && !"".equals(rukuCheck.getJsr())){
            sql.append(" and jsr like '%").append(rukuCheck.getJsr()).append("%'");
        }
        if (rukuCheck.getOnTime()!=null && !"".equals(rukuCheck.getOnTime())){
            sql.append(" and r.rdate >= '").append(rukuCheck.getOnTime()).append("'");
        }
        if (rukuCheck.getOffTime()!=null && !"".equals(rukuCheck.getOffTime())){
            sql.append(" and r.rdate <= '").append(rukuCheck.getOffTime()).append("'");
        }
        List<List> lists = JDBCUtil.queryList(sql.toString());
        if(lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }
    /**
     * 查询一条入库单的详情信息
     * @param rukuCheck
     * @return
     */
    @Override
    public List<Map<String, Object>> getoneruku(RukuCheck rukuCheck) {
        String sql = "SELECT r.rdid,r.rukuid,b.bookname,b.author,b.publisher,r.count,r.pushprice from t_rukudetail r\n" +
                "JOIN t_book b\n" +
                "on r.bookid = b.bookid\n" +
                "where r.rukuid = ? limit ?,?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql,rukuCheck.getRukuid(),(rukuCheck.getPage()-1)*rukuCheck.getLimit(),rukuCheck.getLimit());
        if(maps.size()>0){
            return maps;
        }
        return null;
    }
    /**
     * 查询一条入库信息详情信息的数量
     * @param rukuCheck
     * @return
     */
    @Override
    public int getoneCount(RukuCheck rukuCheck) {
        String sql = "select count(*) from t_rukudetail where rukuid = ?";
        List<List> lists = JDBCUtil.queryList(sql,rukuCheck.getRukuid());
        if(lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }
    /**
     * 审核通过
     * @param rukuCheck
     * @return
     */
    @Override
    public int statusyes(RukuCheck rukuCheck) {
        String sql = "update t_ruku set rdate = ?,status = 1 where rukuid = ?";
        int update = JDBCUtil.update(sql,new Date(), rukuCheck.getRukuid());
        return update;
    }
    /**
     * 审核不通过
     * @param rukuCheck
     * @return
     */
    @Override
    public int statusno(RukuCheck rukuCheck) {
        String sql = "update t_ruku set status = 2 where rukuid = ?";
        int update = JDBCUtil.update(sql, rukuCheck.getRukuid());
        return update;
    }
    /**
     * 添加不通过的原因
     * @param rukuCheck
     * @return
     */
    @Override
    public int addcause(RukuCheck rukuCheck) {
        String sql = "insert into t_rukucheck values(null,?,?)";
        int update = JDBCUtil.update(sql, rukuCheck.getRukuid(),rukuCheck.getCause());
        return update;
    }
    /**
     * 修改相应书籍的数量
     * @param count
     * @param bookid
     * @return
     */
    @Override
    public int updateBookCount(Integer count, Integer bookid) {
        String sql = "update t_book set count = ? where bookid = ?";
        int update = JDBCUtil.update(sql,count,bookid);
        return update;
    }
    /**
     * 获取相应书籍的原有数量
     * @param bookid
     * @return
     */
    @Override
    public int getBookCount(Integer bookid) {
        String sql = "select count from t_book where bookid = ?";
        List<List> lists = JDBCUtil.queryList(sql, bookid);
        if(lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }
    /**
     * 获取入库单详情
     * @param rukuCheck
     * @return
     */
    @Override
    public List<Map<String, Object>> getrukuxiangqing(RukuCheck rukuCheck) {
        String sql = "SELECT r.rdid,r.rukuid,b.bookname,b.author,b.publisher,r.count,r.pushprice,b.bookid from t_rukudetail r\n" +
                "JOIN t_book b\n" +
                "on r.bookid = b.bookid\n" +
                "where r.rukuid = ?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql,rukuCheck.getRukuid());
        if(maps.size()>0){
            return maps;
        }
        return null;
    }
    /**
     * 查询所有不通过的入库申请
     * @param rukuCheck
     * @return
     */
    @Override
    public List<Map<String, Object>> queryAllnotruku(RukuCheck rukuCheck) {
        StringBuilder sql = new StringBuilder("select r.rukuid,rdate,staffName,totalprice,totalcount,jsr,n.cause\n" +
                "from t_ruku r \n" +
                "join t_staff s\n" +
                "on s.id = r.uid JOIN t_rukucheck n ON r.rukuid = n.rukuid where 1=1 and r.status = 2");
        if (rukuCheck.getStaffName()!=null && !"".equals(rukuCheck.getStaffName())){
            sql.append(" and staffName like '%").append(rukuCheck.getStaffName()).append("%'");
        }
        if (rukuCheck.getJsr()!=null && !"".equals(rukuCheck.getJsr())){
            sql.append(" and jsr like '%").append(rukuCheck.getJsr()).append("%'");
        }
        if (rukuCheck.getOnTime()!=null && !"".equals(rukuCheck.getOnTime())){
            sql.append(" and r.rdate >= '").append(rukuCheck.getOnTime()).append("'");
        }
        if (rukuCheck.getOffTime()!=null && !"".equals(rukuCheck.getOffTime())){
            sql.append(" and r.rdate <= '").append(rukuCheck.getOffTime()).append("'");
        }
        sql.append(" limit ?,?");
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql.toString(),(rukuCheck.getPage()-1)*rukuCheck.getLimit(),rukuCheck.getLimit());
        if(maps.size()>0){
            return maps;
        }
        return null;
    }
    /**
     * 查询所有不通过的入库申请的详情
     * @param rukuCheck
     * @return
     */
    @Override
    public int getAllnotCount(RukuCheck rukuCheck) {
        StringBuilder sql = new StringBuilder("select count(rukuid)\n" +
                "from t_ruku r \n" +
                "join t_staff s\n" +
                "on s.id = r.uid where 1=1 and r.status = 2");
        if (rukuCheck.getStaffName()!=null && !"".equals(rukuCheck.getStaffName())){
            sql.append(" and staffName like '%").append(rukuCheck.getStaffName()).append("%'");
        }
        if (rukuCheck.getJsr()!=null && !"".equals(rukuCheck.getJsr())){
            sql.append(" and jsr like '%").append(rukuCheck.getJsr()).append("%'");
        }
        if (rukuCheck.getOnTime()!=null && !"".equals(rukuCheck.getOnTime())){
            sql.append(" and r.rdate >= '").append(rukuCheck.getOnTime()).append("'");
        }
        if (rukuCheck.getOffTime()!=null && !"".equals(rukuCheck.getOffTime())){
            sql.append(" and r.rdate <= '").append(rukuCheck.getOffTime()).append("'");
        }
        List<List> lists = JDBCUtil.queryList(sql.toString());
        if(lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }
}
